package org.jeecg.eqp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.api.CommonAPI;
import org.jeecg.eqp.dto.EqpspchkParamVO;
import org.jeecg.eqp.entity.EqpSpchkCfg;
import org.jeecg.eqp.mapper.EqpSpchkCfgMapper;
import org.jeecg.eqp.service.IEqpSpchkCfgService;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @Description: 点检项配置
 * @Author: shaolong
 * @Date: 2022-10-13
 * @Version: V1.0
 */
@Service
public class EqpSpchkCfgServiceImpl extends ServiceImpl<EqpSpchkCfgMapper, EqpSpchkCfg> implements IEqpSpchkCfgService {
    @Autowired
    private CommonAPI commonApi;
    @Override
    public Result<String> edit(EqpSpchkCfg eqpSpchkCfg) {
        LambdaUpdateWrapper<EqpSpchkCfg> eq = new LambdaUpdateWrapper<EqpSpchkCfg>();
        eq.eq(EqpSpchkCfg::getRecId, eqpSpchkCfg.getRecId());
        boolean update = update(eqpSpchkCfg, eq);
        return update ? Result.ok("修改成功!") : Result.error("修改失败!");
    }

    @Override
    public Result<String> batchSpchkCfg(EqpspchkParamVO eqpspchkParamVO) {
        String serialno = eqpspchkParamVO.getSerialno();
        String trigbus = eqpspchkParamVO.getTrigbus();
        List<EqpSpchkCfg> eqpSpchkCfgList = eqpspchkParamVO.getEqpSpchkCfgList();
        eqpSpchkCfgList.forEach(item->{
            item.setEquipid(serialno);
            item.setTyp(trigbus);
            if (Objects.nonNull(item.getRecId())){
                this.updateById(item);
            }else {
                Integer counterValue = commonApi.getCounterValue("recid.eqp_eqpchk_cfg","0");
                item.setRecId(counterValue);
                this.save(item);
            }
        });
        return Result.OK("OK");
    }

    @Override
    public List<EqpSpchkCfg> queryListByEquipInfo(String equipid) {
        QueryWrapper<EqpSpchkCfg> wrapper = new QueryWrapper<EqpSpchkCfg>()
                .eq("equipid", equipid);
        return this.list(wrapper);
    }
}
